package a66weding.com.jiehuntong.activity.home.hotel.hotelnew;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.x;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.activity.home.HomeStoreDetailsActivity;
import a66weding.com.jiehuntong.adapter.MyBaseAdapter;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HomeProductAnliBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;
import a66weding.com.jiehuntong.widgets.dialog.MyRedheartDialog;

/**
 * Created by admin on 2016/9/12.
 */
public class AnliAdapter extends MyBaseAdapter implements OnNetResultListener {
    private Context mContext;
    private int i_id = 0;
    private boolean isLogin;

    private int mCurrenti_businessId2;
    private boolean isCollectionStare;

    private final int DIALOG = 1;
    private MyRedheartDialog myDialog;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DIALOG:
                    if (myDialog != null) {
                        myDialog.dismiss();
                    }
                    break;
            }
        }
    };

    public AnliAdapter(Context context, List data) {
        super(context, data);
        //是否登录
        isLogin = ((GlobalApplication) context.getApplicationContext()).isLogin();
        if (isLogin) {
            i_id = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
        }
        this.mContext=context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_category_case, null);
            //大图
            viewHolder.itme_case_attention = (ImageView) convertView.findViewById(R.id.itme_case_attention);
            //头像
            viewHolder.iv_homebus_markimg = (ImageView) convertView.findViewById(R.id.iv_homebus_markimg);
            //店铺名字
            viewHolder.tv_homebusitem_hotalname = (TextView) convertView.findViewById(R.id.tv_homebusitem_hotalname);
            //推荐内容
            viewHolder.cate_homecustom_talk = (TextView) convertView.findViewById(R.id.cate_homecustom_talk);
            //关注的容器
            viewHolder.item_case_attention = (RelativeLayout) convertView.findViewById(R.id.item_case_attention);
            viewHolder.cate_attention = (ImageView) convertView.findViewById(R.id.cate_attention);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
         HomeProductAnliBean.DataBean dataBean = (HomeProductAnliBean.DataBean) data.get(position);
        //是否被关注
        final boolean isAttention = dataBean.getBusiness().isCollectionStare();
        int mCurrenti_businessId = dataBean.getI_BusinessId();
        x.image().bind(viewHolder.itme_case_attention, dataBean.getNvc_Pic(), ImageOptionsUtil.getOptions316_720());
        x.image().bind(viewHolder.iv_homebus_markimg, dataBean.getBusiness().getNvc_Logo(), ImageOptionsUtil.getcircle_fixy());
        viewHolder.tv_homebusitem_hotalname.setText(dataBean.getBusiness().getNvc_StoreName());
        viewHolder.cate_homecustom_talk.setText(dataBean.getNvc_Title());

        if (isAttention){  // 如果已经关注
            viewHolder.cate_attention.setImageResource(R.mipmap.img_home_already_attention);
            viewHolder.cate_attention.setTag(R.id.tag_second,true);
        } else {    // 如果没有关注
            viewHolder.cate_attention.setImageResource(R.mipmap.attentiontext);
            viewHolder.cate_attention.setTag(R.id.tag_second,false);
        }

        //点击关注;
        viewHolder.cate_attention.setTag(R.id.tag_first, position);
        viewHolder.cate_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int meposition = (int) v.getTag(R.id.tag_first);
                boolean isAttention = (boolean) v.getTag(R.id.tag_second);
                List<HomeProductAnliBean.DataBean> listData =data;
                mCurrenti_businessId2 = listData.get(meposition).getI_BusinessId();
                if (isLogin) {           // 如果已经登陆
                    if (isAttention) {
                        String cancleUrl = RequestCode.post_CancelBusinessMember;
                        cancleUrl += "&MemberID=" + i_id + "&BusinessID=" + mCurrenti_businessId2;
                        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, cancleUrl,
                                AnliAdapter.this, CommonResultBean.class, RequestCode.post_CancelBusinessMember);
                        HttpHandleUtil.executeCommonPost(mContext, httpRequestBean);
                        isCollectionStare = false;

                    } else {//关注

                        String oldUrl = RequestCode.post_BusinessMemberCreate;
                        oldUrl += "&MemberID=" + i_id + "&BusinessID=" + mCurrenti_businessId2;
                        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, oldUrl,
                                AnliAdapter.this, CommonResultBean.class, RequestCode.post_BusinessMemberCreate);
                        HttpHandleUtil.executeCommonPost(mContext, httpRequestBean);
                        isCollectionStare = true;
                    }

                } else {        //没有登陆提示登陆
                    showLoginDialog(mContext, "亲，需要登录哦~", "去登录", "取消");
                }
            }
        });

        viewHolder.itme_case_attention.setTag(dataBean.getI_Id());
        viewHolder.itme_case_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeStoreDetailsActivity.class);

                int i_id = (int) v.getTag();
                intent.putExtra("casesID", i_id);
                intent.putExtra("isAttention", isAttention);
                ((HomeHotelActivity2) context).startActivityForResult(intent, HomeHotelActivity2.getAttentionRes);
            }
        });
        return convertView;
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof CommonResultBean) {
            CommonResultBean commonData = (CommonResultBean) data;
            switch (commonData.getResCode()) {
                case "0000":
                    if (isCollectionStare) {
                        myDialog = new MyRedheartDialog(mContext, "关注成功!");
                        myDialog.show();
                        mHandler.sendEmptyMessageDelayed(DIALOG, 800);
                        List<HomeProductAnliBean.DataBean> listData =super.data;
                        if (listData != null) {
                            for (int i = 0; i < listData.size(); i++) {
                                int i_businessId = listData.get(i).getI_BusinessId();
                                if (mCurrenti_businessId2 == i_businessId) {
                                    listData.get(i).getBusiness().setCollectionStare(true);
                                }
                            }
                        }

                    } else {
                        myDialog = new MyRedheartDialog(mContext, "成功取消关注!");
                        myDialog.show();
                        mHandler.sendEmptyMessageDelayed(DIALOG, 800);
                        List<HomeProductAnliBean.DataBean> listData =super.data;
                        if (listData != null) {
                            for (int i = 0; i < listData.size(); i++) {
                                int i_businessId = listData.get(i).getI_BusinessId();
                                if (mCurrenti_businessId2 == i_businessId) {
                                    listData.get(i).getBusiness().setCollectionStare(false);
                                }
                            }
                        }
                    }

                  notifyDataSetChanged();

                    break;
                case "0003":
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    class ViewHolder {
        //大图片
        ImageView itme_case_attention;
        //头像
        ImageView iv_homebus_markimg;
        //店铺名字
        TextView tv_homebusitem_hotalname;
        //推荐内容
        TextView cate_homecustom_talk;
        //关注
        RelativeLayout item_case_attention;
        ImageView cate_attention;
    }


    //登录的对话框;
    public void showLoginDialog(final Context context, String title, String confirm, final String cancel) {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(context, title, confirm, cancel, R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent(context, LoginActivity.class);
                context.startActivity(intent);
                confirmDialog.dismiss();
            }

            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }

    /**
     * 取消关注;
     */
    private void cancleAttention(int mCurrenti_businessId) {
        String cancleUrl = RequestCode.post_CancelBusinessMember;
        cancleUrl += "&MemberID=" + this.i_id + "&BusinessID=" + mCurrenti_businessId;
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, cancleUrl,
                this, CommonResultBean.class, RequestCode.post_CancelBusinessMember);
        HttpHandleUtil.executeCommonPost(context, httpRequestBean);
    }

    private void addAttention(int mCurrenti_businessId) {
        String oldUrl = RequestCode.post_BusinessMemberCreate;
        oldUrl += "&MemberID=" + this.i_id + "&BusinessID=" + mCurrenti_businessId;
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, oldUrl,
                this, CommonResultBean.class, RequestCode.post_BusinessMemberCreate);
        HttpHandleUtil.executeCommonPost(context, httpRequestBean);
    }

}
